package cn.itsource.pet.controller;

import cn.itsource.base.util.AjaxResult;
import cn.itsource.base.util.JwtUtils;
import cn.itsource.base.util.LoginData;
import cn.itsource.base.util.Payload;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.domain.dto.AcceptDto;
import cn.itsource.pet.domain.dto.SearchDto;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.system.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    @GetMapping("/getMyPublishPets/{id}")
    public AjaxResult getMyPublishPets(@PathVariable("id")Long userId){
        return searchMasterMsgService.getMyPublishPets(userId);
    }

    /**
     * 拒单
     * @param searchMsgId
     * @return
     */
    @GetMapping("/reject/{searchMsgId}")
    public AjaxResult reject(@PathVariable("searchMsgId")Long searchMsgId){
        return searchMasterMsgService.reject(searchMsgId);
    }

    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptDto acceptDto,HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        Payload<LoginData> loginDataPayload = JwtUtils.checkToken(token);
        LoginData loginData = loginDataPayload.getLoginData();
        Logininfo logininfo = loginData.getLogininfo();
        return searchMasterMsgService.accept(acceptDto,logininfo.getId());
    }

    @PostMapping("/toHandle")
    public AjaxResult toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        Payload<LoginData> loginDataPayload = JwtUtils.checkToken(token);
        LoginData loginData = loginDataPayload.getLoginData();
        Logininfo logininfo = loginData.getLogininfo();
        return searchMasterMsgService.toHandle(query,logininfo.getId());
    }

    /**
     * 查询寻主池的消息
     */
    @PostMapping("/toSearchMasterPool")
    public AjaxResult toSearchMasterPool(@RequestBody SearchMasterMsgQuery query){
        return searchMasterMsgService.toSearchMasterPool(query);
    }

    /**
     * 查询寻主池的消息
     */
    @PostMapping("/shopReceivePet")
    public AjaxResult shopReceivePet(@RequestBody SearchDto dto, HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        Payload<LoginData> loginDataPayload = JwtUtils.checkToken(token);
        LoginData loginData = loginDataPayload.getLoginData();
        Logininfo logininfo = loginData.getLogininfo();
        return searchMasterMsgService.shopReceivePet(dto,logininfo.getId());
    }

    @PostMapping("/publish")
    public AjaxResult publish(@Valid @RequestBody SearchMasterMsg searchMasterMsg){
        return searchMasterMsgService.publish(searchMasterMsg);
    }


    /**
     * 接口：添加或修改
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){ 
        if( searchMasterMsg.getId()!=null){
		searchMasterMsgService.update(searchMasterMsg);
        }else{
                searchMasterMsgService.save(searchMasterMsg);
	}
        return new AjaxResult();
       
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        searchMasterMsgService.deleteById(id);
        return new AjaxResult();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        searchMasterMsgService.batchDeleteByIds(ids);
        return new AjaxResult();
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public SearchMasterMsg findOne(@PathVariable("id")Long id) {
        return searchMasterMsgService.selectById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<SearchMasterMsg> findAll(){
        return searchMasterMsgService.selectAll();
    }



}
